package com.gainsoft.grevocabuilder;

import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class VocaBuilder extends Activity
{
    Cursor c = null;
    Word curentWord = null;
    public static Context context;

    View.OnClickListener handler = new View.OnClickListener()
    {
	public void onClick(View v)
	{
	    switch (v.getId())
	    {
	    case R.id.LeftBtn: // doStuff
		break;
	    case R.id.RightBtn: // doStuff
		break;
	    }

	}
    };

    void LoadMeaningInRadioBtn()
    {
	List<Word> wordList = WordList.getRandomListofWord();
	RadioButton tx0 = (RadioButton) findViewById(R.id.Meaning0);
	RadioButton tx1 = (RadioButton) findViewById(R.id.Meaning1);
	RadioButton tx2 = (RadioButton) findViewById(R.id.Meaning2);
	RadioButton tx3 = (RadioButton) findViewById(R.id.Meaning3);
	wordList.add(curentWord);
	Collections.shuffle(wordList);
	tx0.setText(wordList.get(0).getMeaning());
	tx0.setTextColor(Color.BLACK);
	tx1.setText(wordList.get(1).getMeaning());
	tx1.setTextColor(Color.BLACK);
	tx2.setText(wordList.get(2).getMeaning());
	tx2.setTextColor(Color.BLACK);
	tx3.setText(wordList.get(3).getMeaning());
	tx3.setTextColor(Color.BLACK);

    }

    /** Called when the activity is first created. */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	context = getApplicationContext();
	Spinner spinner = (Spinner) findViewById(R.id.alphabets);

	@SuppressWarnings("rawtypes")
	ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,
		android.R.layout.simple_spinner_item, getResources()
			.getStringArray(R.array.alphabets))
	{
	    @Override
	    public View getDropDownView(int position, View convertView,
		    ViewGroup parent)
	    {
		Resources res = getResources();
		View view = super
			.getDropDownView(position, convertView, parent);
		if (position % 2 == 0)
		{
		    view.setBackgroundColor(res.getColor(R.color.altrow));
		} else
		{
		    view.setBackgroundColor(res.getColor(R.color.row));
		}

		return view;
	    }

	};

	TextView tx = (TextView) findViewById(R.id.WordView);

	c = WordList.getWordCursorByAlphabet("ALL", getApplicationContext());
	curentWord = new Word(c);
	LoadMeaningInRadioBtn();
	tx.setText(curentWord.getWord());
	tx.setTextColor(Color.BLACK);
	spinner.setAdapter(adapter);

	ImageButton lftImgBttn = (ImageButton) findViewById(R.id.LeftBtn);
	lftImgBttn.setOnClickListener(new View.OnClickListener()
	{
	    public void onClick(View v)
	    {
		LoadMeaningInRadioBtn();
		if (curentWord.previousWord())
		{
		    TextView tx = (TextView) findViewById(R.id.WordView);
		    tx.setText(curentWord.getWord());
		    tx.setTextColor(Color.BLACK);

		}
	    }
	});

	ImageButton rgtImgBttn = (ImageButton) findViewById(R.id.RightBtn);
	rgtImgBttn.setOnClickListener(new View.OnClickListener()
	{
	    public void onClick(View v)
	    {
		LoadMeaningInRadioBtn();
		if (curentWord.nextWord())
		{
		    TextView tx = (TextView) findViewById(R.id.WordView);
		    tx.setText(curentWord.getWord());
		    tx.setTextColor(Color.BLACK);
		}
	    }
	});

	spinner.setOnItemSelectedListener(new OnItemSelectedListener()
	{

	    @Override
	    public void onItemSelected(AdapterView<?> parent, View view,
		    int pos, long id)
	    {
		c = WordList.getWordCursorByAlphabet(
			parent.getItemAtPosition(pos).toString().toLowerCase(),
			getApplicationContext());
		curentWord = new Word(c);
		LoadMeaningInRadioBtn();
		TextView tx = (TextView) findViewById(R.id.WordView);
		tx.setText(curentWord.getWord());
		tx.setTextColor(Color.BLACK);

	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> parent)
	    {
		// TODO Auto-generated method stub

	    }

	});

    }
}